/**
*  \file mcu.h
*
*  \brief Controller abstraction Layer
*
*  \author Michael J. Hohmann <michael.hohmann@linde-mh.de>
*
*   This Unit provide an interface to the application to use hardware features.
*
*/
#ifndef MCU_H_INCLUDED
#define MCU_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"
#include "../i2cgi_sim/i2cgi_sim.h"
/*================================================[ public defines   ]================================================*/

/*================================================[ public datatypes ]================================================*/


#define MCU_GPIO_PORT( port, pin )     ((port<<8)|pin)


/** Digital inputs */
enum mcu_input
{
    EN_PARK_BRAKE_IN_D_CPU_V            = MCU_GPIO_PORT( 3, 3 ), /**< PARK_BRAKE_IN_D_CPU_V      */
    EN_RESERVE_1_IN_D_CPU_V             = MCU_GPIO_PORT( 2, 0 ), /**< RESERVE_1_IN_D_CPU_V       */
    EN_MAIN_CONTACTOR_ERROR             = MCU_GPIO_PORT( 2, 8 ), /**< MAIN_CONTACTOR_ERROR       */
    EN_CUT_OFF_ISO_VALVE_ERROR          = MCU_GPIO_PORT( 2, 9 ), /**< CUT-OFF_ISO_VALVE_ERROR    */
    EN_BUZZER_ERROR                     = MCU_GPIO_PORT( 2, 10), /**< BUZZER_ERROR               */
    EN_FAN_ERROR                        = MCU_GPIO_PORT( 5, 2 ), /**< FAN_ERROR                  */
    EN_FORWARD_IN_D_CPU_V               = MCU_GPIO_PORT( 0, 1 ), /**< FORWARD_IN_D_CPU_V         */
    EN_BOOT_ANCHOR_CPU_M                = MCU_GPIO_PORT( 1, 0 ), /**< /BOOT_ANCHOR_CPU_M         */
    EN_SEAT_SWITCH_NC_IN_D_CPU_V        = MCU_GPIO_PORT( 1, 1 ), /**< SEAT_SWITCH_NC_IN_D_CPU_V  */
    EN_NEUTRAL_POSITION_IN_D_CPU_V      = MCU_GPIO_PORT( 1, 8 ), /**< NEUTRAL_POSITION_IN_D_CPU_V*/
    EN_REVERSE_IN_D_CPU_V               = MCU_GPIO_PORT( 1, 10), /**< REVERSE_IN_D_CPU_V         */
    EN_LIFT_IN_D_CPU_V                  = MCU_GPIO_PORT( 1, 11), /**< LIFT_IN_D_CPU_V            */
    EN_TILT_IN_D_CPU_V                  = MCU_GPIO_PORT( 1, 12), /**< TILT_IN_D_CPU_V            */
    EN_MAST_AUX_1_IN_D_CPU_V            = MCU_GPIO_PORT( 1, 13), /**< MAST_AUX_1_IN_D_CPU_V      */
    EN_MAST_AUX_2_IN_D_CPU_V            = MCU_GPIO_PORT( 1, 14), /**< MAST_AUX_2_IN_D_CPU_V      */
    EN_SEAT_SWITCH_IN_D_CPU_V           = MCU_GPIO_PORT( 1, 15), /**< SEAT_SWITCH_IN_D_CPU_V     */
    EN_TLF_SAFESTATE1                         = MCU_GPIO_PORT( 3, 4 ), /**<                            */
    EN_TLF_ERR                               = MCU_GPIO_PORT( 3, 5 )  /**<                            */
};

/** Digital Outputs */
enum mcu_output
{
    //mcu_output_CUT_OFF_ISO_VALVE_OUT_LS_CPU_V  = MCU_GPIO_PORT( 0, 0  ), is a pwm output
    mcu_output_FAN_CONTROLLER_OUT_LS_CPU_V         = MCU_GPIO_PORT( 0, 2  ), /**<                      */
    mcu_output_DCDC_CONTROL_CPU_V                     = MCU_GPIO_PORT( 0, 4  ), /**<                      */
    mcu_output_BUZZER_OUT_LS_CPU_V                 = MCU_GPIO_PORT( 0, 9  ), /**<                      */
    mcu_output_FAN_MOTOR_OUT_LS_CPU_V              = MCU_GPIO_PORT( 0, 10 ), /**<                      */
    mcu_output_DC_LINK_PRECHAGE_EN_CPU_V           = MCU_GPIO_PORT( 1, 6  ), /**<                      */
    mcu_output_RESERVE_2_IN_ON_CPU_V                  = MCU_GPIO_PORT( 1, 7  ), /**<                      */
    mcu_output_INV1_ENABLE_CPU_V                      = MCU_GPIO_PORT( 1, 9  ), /**<                      */
    mcu_output_INV2_ENABLE_CPU_V                      = MCU_GPIO_PORT( 2, 14 ), /**<                      */
    mcu_output_ALARM_BUZZER_OUT_LS_CPU_V           = MCU_GPIO_PORT( 2, 14 ), /**<                      */
    mcu_output_LED_0805_GE                       = MCU_GPIO_PORT( 3, 6  ), /**< LED_0805_GE          */
    mcu_output_I2C_WP_CPU_M                           = MCU_GPIO_PORT( 0, 12 ), /**<                      */
    mcu_output_RESET_ERROR_CPU_V                 = MCU_GPIO_PORT( 5, 7  ), /**< RESET_ERROR_CPU_V    */
};

/** ADC Channels */
enum mcu_adc
{   /* do not change order. this order is the order of the input ports */
       mcu_adc_12VSupply         ,   /**< 0  12V_DRIVER_SUPPLY                         */
       mcu_adc_TempMotInv2       ,   /**< 1  TEMP_MOT_INV2_IN_A_CPU_V                  */
       mcu_adc_5VExtSns          ,   /**< 2  5V_EXTERNAL_SENSORS_A_CPU_V               */
       mcu_adc_Reserve1          ,   /**< 3  RESERVE_1_IN_A_CPU_V                      */
       mcu_adc_lift_Pot          ,   /**< 4  RESERVE_2_IN_A_CPU_V                      */
       mcu_adc_DcLink            ,   /**< 5  DC_LINK_+_IN_A_CPU_V                      */
       mcu_adc_Accelerator1      ,   /**< 6  ACCELERATOR_1_IN_A_CPU_V                  */
       mcu_adc_13_7VSupply       ,   /**< 7  +13.7V_SUPPLY_A_CPU_V                     */
       mcu_adc_SteerAngleB       ,   /**< 8  STEER_ANGLE_B_IN_A_CPU_V                  */
       mcu_adc_Reserve3          ,   /**< 9  RESERVE_3_IN_A_CPU_V                      */
       mcu_adc_TempMotInv1       ,   /**< 10 TEMP_MOT_INV1_IN_A_CPU_V                  */
       mcu_adc_CurrentCutOffIso  ,   /**< 11 CURRENT_CUT-OFF_ISO_VALVE_A_CPU_V         */
       mcu_adc_Accelerator2      ,   /**< 12 ACCELERATOR_2_IN_A_CPU_V                  */
       mcu_adc_CurrentMainCont   ,   /**< 13 CURRENT_MAIN_CONTACTOR_A_CPU_V            */
       mcu_adc_Reserve4          ,   /**< 14 RESERVE_4_IN_A_CPU_V                      */
       mcu_adc_SteerAngleA       ,   /**< 15 STEER_ANGLE_A_IN_A_CPU_V                  */
       mcu_adc_Reserve5          ,   /**< 16 RESERVE_4_IN_A_CPU_V                      */
       mcu_adc_KeySwitch         ,   /**< 17 KEY_SWITCH_IN_A_CPU_V                     */
       mcu_adc_max_chn
};

/** PWM outputs */
enum mcu_pwm
{
   mcu_pwm_contactor    ,
   mcu_pwm_iso_valve    ,
   mcu_pwm_max
};

/*================================================[ public variables ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
/** Initialization.
*
*  Initialize the mcu module.
*
*  \return RC_SUCCESS if successful.
*/
RC mcu_initialize( void );

/** Read Adc Channel
* \return current value of adc channel - No filter no average
*/
U16 mcu_get_adc( enum mcu_adc  channel );

/** Read input
*
* \param input pin
* \return TRUE pin is High - FALSE pin is low
*/
BO mcu_get_input( enum mcu_input pin );

/** Set Output
*
*/
void mcu_set_output( enum mcu_output pin , BO value );

/** Set PWM output
*
*/
void mcu_set_pwm(enum mcu_pwm channel, U16 value );

/** Get Reset Reason
*
* \return Reset Reason
*/
U32 mcu_get_reset_reason(void);

/** Get High resoltuion tick counter
*
* \return Tick Counter
*/
U32 mcu_high_resolution_counter(void);

/** Transmit data on I2c
*
*  \param cmd Specify if start and/or stop bit should be set
*  \param control control byte contains read/write and address information (will only be send if stat conditio is selected )
*  \param data Pointer to data to transmit or pointer to buffer to receive data ( depending on control word )
*  \param len  Number of bytes to receive / transmit
*  \return TRUE if acknowledge was send by slave
*/
BO mcu_i2c_transmit(enum i2cgi_cmd cmd, U8 control, void* data, pal_size_t len );

/* only for simulation */


U16 mcu_get_port_out_sim( U16 port );

U32 mcu_get_build_time_app(void);
U32 mcu_get_build_time_bb(void);
F32 mcu_get_motor_speed(void);

/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2019 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
